package ma.ensao.evalmetrics.interpretation;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import ma.ensao.evalmetrics.controller.MetriquesManager;
import ma.ensao.evalmetrics.controller.ProprieteManager;
import ma.ensao.evalmetrics.controller.ResultManager;
import ma.ensao.evalmetrics.controller.SubcaracManager;
import ma.ensao.evalmetrics.controller.metrics.Calcule;
import ma.ensao.evalmetrics.model.Metriques;
import ma.ensao.evalmetrics.model.Propriete;
import ma.ensao.evalmetrics.model.ProprieteInt;
import ma.ensao.evalmetrics.model.Result;
import ma.ensao.evalmetrics.model.Subcarac;
import ma.ensao.evalmetrics.model.SubcaracInt;

import com.opensymphony.xwork2.ActionSupport;

public class SubcaracIntManager extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long idp;
	private ResultManager resultmanager;
	private Result maresult;
	private MetriquesManager metriquesmanager = new MetriquesManager();
	private ProprieteManager proprietemanager = new ProprieteManager();
	private SubcaracManager subcarac = new SubcaracManager();
	private Calcule calcule = new Calcule();
	private Propriete propriete;
	private Metriques metric;
	private Subcarac sub;
	private List<SubcaracInt> records;
	private String result;
	
	//
	private Long idresult;

public String afficher() {
		System.out.println("idp "+idp+" idres "+idresult);
		records = new ArrayList<SubcaracInt>() ;
		resultmanager = new ResultManager();
		maresult = resultmanager.resultById(idresult);
		metric = metriquesmanager.list(idp).get(0);
		metric = calcule.calculMetrique(metric, maresult);
		propriete = proprietemanager.list(idp).get(0);
		propriete = calcule.calculPropriete(propriete);
		propriete.setId(idresult);
		sub = subcarac.list(idp).get(0);
		sub = calcule.calculSubcarac(sub);
		
		ProprieteInt proprieteInt;
		proprieteInt = ProprieteIntManager.map(propriete,metric);
		SubcaracInt subint;
		subint = map(sub,proprieteInt);
		
		records.add(subint);
		
		result="OK";

		return SUCCESS;
	}

	public static SubcaracInt map(Subcarac oldsub, ProprieteInt prop){
		SubcaracInt sub = new SubcaracInt();
		
		sub.setConvenance(oldsub.getConvenance());
		sub.setExactitude(oldsub.getExactitude());
		sub.setInteroperabilite(oldsub.getInteroperabilite());
		sub.setSecurite(oldsub.getSecurite());
		sub.setCompliance(oldsub.getCompliance());
		sub.setMaturite(oldsub.getMaturite());
		sub.setFausseTolerance(oldsub.getFausseTolerance());
		sub.setRecoverabilite(oldsub.getRecoverabilite());
		sub.setClarete(oldsub.getClarete());
		sub.setLearnabilite(oldsub.getLearnabilite());
		sub.setAttractivite(oldsub.getAttractivite());
		sub.setComportement_time(oldsub.getComportement_time());
		sub.setUtilisation_Ressource(oldsub.getUtilisation_Ressource());
		sub.setVariabilite(oldsub.getVariabilite());
		sub.setAnalyse(oldsub.getAnalyse());
		sub.setStabilite(oldsub.getStabilite());
		sub.setTestabilite(oldsub.getTestabilite());
		sub.setAdabtabilite(oldsub.getAdabtabilite());
		sub.setInstabilite(oldsub.getInstabilite());
		sub.setConformite(oldsub.getConformite());
		sub.setRemplacabilite(oldsub.getRemplacabilite());
		
		// Interoperabilite
		double interp = (prop.getCouplingInt()+prop.getHeritageInt())/2;
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		sub.setInteroperabiliteInt(interp);
		// securite
		interp = (prop.getCouplingInt()+prop.getHeritageInt()+prop.getAbstractionInt()+prop.getEncapsulationInt())/4;
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		sub.setSecuriteInt(interp);
		// maturite
		interp = prop.getCohesionInt();
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		sub.setMaturiteInt(interp);
		//FausseTolerance
		interp = (prop.getInstabiliteInt()+prop.getDependenceInt())/2;
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		sub.setFausseToleranceInt(interp);
		//Recoverabilite + Adabtabilite + Testabilite
		interp = prop.getComplexiteInt();
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		sub.setRecoverabiliteInt(interp);
		sub.setAdabtabiliteInt(interp);
		sub.setTestabiliteInt(interp);
		//Attractivite
		interp = prop.getMessageInt();
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		sub.setAttractiviteInt(interp);
		//Variabilite
		interp = (prop.getCouplingInt()+prop.getHeritageInt()+prop.getComplexiteInt())/3;
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		sub.setVariabiliteInt(interp);
		// Analyse
		interp = (prop.getCouplingInt()+prop.getHeritageInt())/2;
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		sub.setAnalyseInt(interp);
		//Stabilite
		interp = (prop.getCouplingInt()+prop.getDependenceInt())/2;
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		sub.setStabiliteInt(interp);
		//Instabilite
		interp = prop.getInstabiliteInt();
		interp = new BigDecimal(interp ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		sub.setInstabiliteInt(interp);
		
		return sub;
	}

	public Long getIdp() {
		return idp;
	}

	public void setIdp(Long idp) {
		this.idp = idp;
	}

	public List<SubcaracInt> getRecords() {
		return records;
	}

	public void setRecords(List<SubcaracInt> records) {
		this.records = records;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public Long getIdresult() {
		return idresult;
	}

	public void setIdresult(Long idresult) {
		this.idresult = idresult;
	}
	
	
}
